package com.aiyige.page.globalsearch.adapter;

import android.text.Html;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aiyige.R;
import com.aiyige.page.globalsearch.model.SearchGroup;
import com.aiyige.utils.GlideUtil;
import com.aiyige.utils.StringUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.LinkedList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by zhuyakun on 2017/12/11.
 */

public class LearnVideoAdapter extends BaseQuickAdapter<SearchGroup.Sample, LearnVideoAdapter.ViewHolder> {
    public LearnVideoAdapter() {
        super(R.layout.global_search_learn_video_item, new LinkedList<SearchGroup.Sample>());
    }

    @Override
    protected void convert(ViewHolder helper, SearchGroup.Sample item) {
        helper.bindData(item);
    }


    public class ViewHolder extends BaseViewHolder {
        SearchGroup.Sample data;
        @BindView(R.id.coverIv)
        ImageView coverIv;
        @BindView(R.id.episodeTv)
        TextView episodeTv;
        @BindView(R.id.durationTv)
        TextView durationTv;
        @BindView(R.id.coverLayout)
        FrameLayout coverLayout;
        @BindView(R.id.avatarIv)
        ImageView avatarIv;
        @BindView(R.id.authorName)
        TextView authorName;
        @BindView(R.id.titleTv)
        TextView titleTv;
        @BindView(R.id.freeTv)
        TextView freeTv;
        @BindView(R.id.priceTv)
        TextView priceTv;
        @BindView(R.id.priceLayout)
        LinearLayout priceLayout;
        @BindView(R.id.priceContainer)
        FrameLayout priceContainer;
        @BindView(R.id.butNumTv)
        TextView butNumTv;

        public ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }

        public void bindData(SearchGroup.Sample item) {
            data = item;
            GlideUtil.with(itemView.getContext())
                    .loadCover(data.getCover())
                    .into(coverIv);
            GlideUtil.with(itemView.getContext())
                    .loadAvatar(data.getOwnerAvatar())
                    .into(avatarIv);
            authorName.setText(Html.fromHtml(data.getOwner()));
            episodeTv.setText(String.format(StringUtils.getString(R.string.total_episode), data.getStages()));
            durationTv.setText(StringUtils.formatMediaDurationMS(data.getDurationTotal()));
//            if (data.getPrice() < 0.01) {
//                priceLayout.setVisibility(View.INVISIBLE);
//                freeTv.setVisibility(View.VISIBLE);
//            } else {
            priceLayout.setVisibility(View.VISIBLE);
            freeTv.setVisibility(View.INVISIBLE);
            priceTv.setText(StringUtils.priceFormat(data.getPrice()));
//            }
            butNumTv.setText(String.format(StringUtils.getString(R.string.buy_num), StringUtils.formatNum((int) data.getSellCount())));
            titleTv.setText(Html.fromHtml(data.getTitle()));
        }
    }
}
